﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerInventoryItemDao : IInventoryItemDao
    {
        public IList<InventoryItem> GetInventoryItems()
        {
            string procedures = "Get_All_InventoryItems";

            return Db.ReadList(procedures, true, Make);
        }

        public IList<InventoryItem> GetInventoryItemsActive(bool isMaterial, bool isActive)
        {
            string procedures = "Get_All_InventoryItems_ByCondition";
            int _isMaterial = 0;
            int _isActive = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            if (isActive) { _isActive = 1; }
            else { _isActive = 0; }

            object[] parms = { "@WhereClause", " WHERE IsActive = " + _isActive + " AND IsMaterial = " + _isMaterial };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public IList<InventoryItem> GetInventoryItemsActive(bool isMaterial, bool isActive, string restaurantId)
        {
            string procedures = "Get_All_InventoryItems_ByCondition";
            int _isMaterial = 0;
            int _isActive = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            if (isActive) { _isActive = 1; }
            else { _isActive = 0; }

            object[] parms = { "@WhereClause", " WHERE IsActive = " + _isActive + " AND IsMaterial = " + _isMaterial + " AND RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public InventoryItem GetInventoryItem(string inventoryItemId)
        {
            string procedures = "Get_All_InventoryItem_ById";

            object[] parms = { "@InventoryItemID", inventoryItemId };
            return Db.Read(procedures, true, Make, parms);
        }

        public IList<InventoryItem> GetInventoryItemsByCategory(string inventoryCategoryId)
        {
            string procedures = "Get_All_InventoryItem_ByCategory";

            object[] parms = { "@InventoryCategoryID", inventoryCategoryId };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public IList<InventoryItem> GetInventoryItemsByPageAndBranch(int currentPage, string restaurantId, bool isMaterial)
        {
            string procedures = "Get_Paged_InventoryItems";
            int _isMaterial = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantId + "' AND IsMaterial = " + _isMaterial, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetCountInventoryItemByCategory(string categoryId)
        {
            string procedures = "Get_InventoryItem_ByCategoryId";

            object[] parms = { "@InventoryCategoryID", categoryId };

            var inventoryItems = Db.ReadList(procedures, true, Make, parms);
            if (inventoryItems != null) { return inventoryItems.Count; }
            else { return 0; }
        }

        public IList<InventoryItem> GetInventoryItemsByPage(int currentPage, bool isMaterial)
        {
            string procedures = "Get_Paged_InventoryItems";
            int _isMaterial = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            object[] parms = {  "@WhereClause", " IsMaterial = " + _isMaterial, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public IList<InventoryItem> GetInventoryItemsByPageAndCategory(int currentPage, string inventoryCategoryId, bool isMaterial)
        {
            string procedures = "Get_Paged_InventoryItems";
            int _isMaterial = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            object[] parms = {  "@WhereClause", " InventoryCategoryID = '" + inventoryCategoryId + "' AND IsMaterial = " + _isMaterial, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPage(bool isMaterial)
        {
            string procedures = "Get_PageSize_InventoryItem";
            int _isMaterial = 0;

            if (isMaterial) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            object[] parms = { "@WhereClause", " WHERE IsMaterial = '" + _isMaterial + "'" };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public int GetTotalPageByCategory(string inventoryCategoryId)
        {
            string procedures = "Get_PageSize_InventoryItem";
            object[] parms = { "@WhereClause", " WHERE InventoryCategoryID = '" + inventoryCategoryId + "'" };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public void InsertInventoryItem(InventoryItem inventoryItem)
        {
            string sql = "Insert_InventoryItem";

            inventoryItem.InventoryItemID = Db.Insert(sql, true, Take(inventoryItem));
        }

        public void UpdateInventoryItem(InventoryItem inventoryItem)
        {
            string sql = "Update_InventoryItem";

            Db.Update(sql, true, Take(inventoryItem));
        }

        public void DeleteInventoryItem(InventoryItem inventoryItem)
        {
            string sql = "Delete_InventoryItem";

            Db.Update(sql, true, Take(inventoryItem.InventoryItemID));
        }

        private object[] Take(InventoryItem inventoryItem)
        {
            return new object[]  
            {
                "@InventoryItemID", inventoryItem.InventoryItemID,
                "@InventoryCategoryID", inventoryItem.InventoryCategoryID,
                "@InventoryItemCode", inventoryItem.InventoryItemCode,
                "@InventoryItemName", inventoryItem.InventoryItemName,
                "@UnitID", inventoryItem.UnitID,
                "@UnitPrice", inventoryItem.UnitPrice,
			    "@SalesPrice", inventoryItem.SalesPrice,
                "@StockID", inventoryItem.StockID ,
                "@MinimumStock", inventoryItem.MinimumStock,
                "@IsActive", inventoryItem.IsActive,
                "@Description", inventoryItem.Description,
                "@RestaurantID", inventoryItem.RestaurantId,
                "@IsMaterial", inventoryItem.IsMaterial
            };
        }

        private object[] Take(string inventoryItemId)
        {
            return new object[]  
            {
                "@InventoryItemID", inventoryItemId,
            };
        }

        private static Func<IDataReader, InventoryItem> Make = reader =>
            new InventoryItem
            {
                InventoryItemID = reader["InventoryItemID"].AsString(),
                InventoryCategoryID = reader["InventoryCategoryID"].AsString(),
                InventoryItemCode = reader["InventoryItemCode"].AsString(),
                InventoryItemName = reader["InventoryItemName"].AsString(),
                UnitID = reader["UnitID"].AsString(),
                UnitPrice = reader["UnitPrice"].AsDecimal(),
                SalesPrice = reader["SalesPrice"].AsDecimal(),
                StockID = reader["StockID"].AsString(),
                MinimumStock = reader["MinimumStock"].AsInt(),
                IsActive = reader["IsActive"].AsBool(),
                Description = reader["Description"].AsString(),
                RestaurantId = reader["restaurantID"].AsString(),
                IsMaterial = reader["IsMaterial"].AsBool(),
            };
    }
}
